package com.example.oms.controller;

import com.example.oms.vo.PrescriptionAndMedListVo;
import com.example.oms.entity.*;
import com.example.oms.service.DoctorService;
import io.swagger.models.Response;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("doc")
public class DocController {
    @Resource
    DoctorService doctorService;

    /**
     * 根据医生id获取问诊队列
     */
    @GetMapping("/getQueueListByDocId")
    public ResponseEntity<List<Map<String,Object>>> getQueueListByDocId(@RequestParam int docId) {
        List<Map<String,Object>> result = doctorService.getQueueListByDocId(docId);
        return ResponseEntity.ok(result);
    }

    /**
     * 按病人id和日期获取当日所有挂号信息
     */
    @GetMapping("/getAllPatientsByIdAndDate")
    public ResponseEntity<List<Map<String, Object>>> getAllPatientsByIdAndDate(@RequestParam int docId,@RequestParam @DateTimeFormat(pattern="yyyyMMdd") Date date) {
        List<Map<String, Object>> result = doctorService.getAllPatientsByIdAndDate(docId, date);
        System.out.println("result"+result);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据病人id获取病人信息
     */
    @GetMapping("/getPatientDetailById")
    public ResponseEntity<Patients> getPatientDetailById(@RequestParam int patientId) {
        Patients patient = doctorService.getPatientDetailById(patientId);
        if (patient == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(patient);
    }

    /**
     * 修改病历信息
     */
    @PostMapping("/updateDiagRecordById")
    public ResponseEntity<String> updateDiagRecordById(@RequestBody DiagRecords diagRecord) {
        doctorService.updateDiagRecord(diagRecord);
        return ResponseEntity.ok("ok");
    }

    /**
     * 按病历id获取处方
     */
    @GetMapping("/getPresByRecordId")
    public ResponseEntity<List<PrescriptionAndMedListVo>> getPresByRecordId(@RequestParam int recordId) {
        List<PrescriptionAndMedListVo> result = doctorService.getPrescriptionByDiagRecordId(recordId);
        return ResponseEntity.ok(result);
    }

    /**
     * 按病人id获取处方
     */
    @GetMapping("/getPresByPatientId")
    public ResponseEntity<List<PrescriptionAndMedListVo>> getPresByPatientId(
            @RequestParam int patientId,
            @RequestParam int pageNum
    ) {
        List<PrescriptionAndMedListVo> result = doctorService.getPresListByPatientId(patientId, pageNum);
        return ResponseEntity.ok(result);
    }

    /**
     * 按处方id获取处方
     * */
    @GetMapping("/getPresByPresId")
    public ResponseEntity<PrescriptionAndMedListVo> getPresByPresId(@RequestParam int presId) {
        PrescriptionAndMedListVo result = doctorService.getPrescriptionByPresId(presId);
        return ResponseEntity.ok(result);
    }

    /**
     * 新增一个处方
     * */
    @PostMapping("/newPres")
    public ResponseEntity<String> newPres(@RequestBody PrescriptionAndMedListVo prescriptionAndMedList) {
        doctorService.newPrescription(prescriptionAndMedList);
        return ResponseEntity.ok("ok");
    }

    /**
     * 新增检查项目
     * */
    @PostMapping("/newDiag")
    public ResponseEntity<String> newDiag(@RequestBody DiagReports diagReports) {
        doctorService.newDiag(diagReports);
        return ResponseEntity.ok("ok");
    }

    /**
     * 根据病历id获取病历下包含的所有检查
     * */
    @GetMapping("/getDiagReportsByRecordId")
    public ResponseEntity<List<DiagReports>> getDiagReportsByRecordId(@RequestParam int recordId) {
        List<DiagReports> result = doctorService.getDiagReportsByDiagRecordId(recordId);
        return ResponseEntity.ok(result);
    }

    /**
     * 获取所有药品
     * */
    @GetMapping("/getAllMeds")
    public ResponseEntity<List<Meds>> getAllMeds(){
        List<Meds> medList = doctorService.getAllMeds();
        return ResponseEntity.ok(medList);
    }

    @PostMapping("/updateRegStatusByRegId")
    public ResponseEntity<String> updateRegStatusByRegId(@RequestParam int regId,@RequestParam String status){
        doctorService.updateRegStatusByRegId(regId,status);
        Registrations regByRegId = doctorService.getRegByRegId(regId);
        if(status.equals("Completed")||status.equals("Canceled")){
            System.out.println("popQueue");
            doctorService.popQueue(regByRegId.getDoctorId(),regByRegId.getAppointmentPeriod());
        }
        return ResponseEntity.ok("ok");
    }

    @GetMapping("/getRecordByRegId")
    public ResponseEntity<DiagRecords> getRecordByRegId(@RequestParam int regId){
        DiagRecords recordByRegId = doctorService.getRecordByRegId(regId);
        return ResponseEntity.ok(recordByRegId);
    }

    @PostMapping("/newDiagRecord")
    public ResponseEntity<Integer> newDiagRecord(@RequestBody DiagRecords diagRecord){
        int newId = doctorService.newDiagRecord(diagRecord);
        return ResponseEntity.ok(newId);
    }
}
